package p2p;

import java.io.Serializable;

public class Peer implements Comparable<Peer>, Serializable{
	private static final long serialVersionUID = -7302045174734353019L;
	private String name;
	private String ip;
	private int port;
	private int maxNumberOfNeighbours;
	private int capacity;
	
	public Peer(String ip, int port) {
		this.ip = ip;
		this.port = port;
	}
	
	public Peer(String name, String ip, int port, int maxNumberOfNeighbours, int capacity) {
		this.name = name;
		this.ip = ip;
		this.port = port;
		this.maxNumberOfNeighbours = maxNumberOfNeighbours;
		this.capacity = capacity;
	}
	
	public int getCapacity() {
		return capacity;
	}

	public void setCapacity(int capacity) {
		this.capacity = capacity;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((ip == null) ? 0 : ip.hashCode());
		result = prime * result + maxNumberOfNeighbours;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + port;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		Peer other = (Peer) obj;
		
		if(other.getIp().equals(this.getIp()) && other.getPort() == this.getPort()) {
			return true;
		}

		return false;
	}
	
	@Override
	public String toString() {
		return this.getName() + "("+this.getMaxNumberOfNeighbours()+")("+this.getCapacity()+") " + this.getIp() + ":" + this.getPort();
	}

	public int getMaxNumberOfNeighbours() {
		return maxNumberOfNeighbours;
	}

	public void setMaxNumberOfNeighbours(int maxNumberOfNeighbours) {
		this.maxNumberOfNeighbours = maxNumberOfNeighbours;
	}
	
	public int compareTo(Peer peer) {
		if(this.getCapacity() > peer.getCapacity()) return 1;
		if(this.getCapacity() < peer.getCapacity()) return -1;
		return  0;
	}
}
